Click here to show the code
knitr::opts_chunk$set(message = FALSE)The following chunk sets up rules for all the chunks in the document. I set the knits options for the chunks so that by default messages generated by R are not displayed.
Please notice the alternative way to name the chunk (within the curly brackets, and not after the #| as we’ve seen before). Similarly, other YAML options that we have seen listed after #| could be just used within the curly brackets. For example `{r chunk_name, include=FALSE}.
knitr::opts_chunk$set(message = FALSE)In this second package I load all the necessary packages. I also use herefor file referencing and automatic working directory setting.
pacman::p_load(here, tidyverse, ggimage, palmerpenguins, dplyr, ggpattern, patchwork, png, rasterImage, grid)
here::i_am("images_in_R_plots.qmd")Here I want to show you how to add images to your plot. There are different ways to do this, depedning on what you would like to achieve.
Choose a tab for different ways to add the logo image to ggplots:
Adapted from Albert Rapp’s blog post on the topic.
The following is a plot using the Palmer Penguins Data Set taken from their examples.
mass_flipper <- ggplot(data = penguins,
aes(x = flipper_length_mm,
y = body_mass_g)) +
geom_point(aes(color = species,
shape = species),
size = 3,
alpha = 0.8) +
scale_color_manual(values = c("darkorange","purple","cyan4")) +
labs(title = "Penguin size, Palmer Station LTER",
x = "Flipper length (mm)",
y = "Body mass (g)",
color = "Penguin species",
shape = "Penguin species") +
theme(legend.position.inside = c(0.2, 0.7),
plot.title.position = "plot",
plot.caption = element_text(hjust = 0, face= "italic"),
plot.caption.position = "plot")
mass_flipperAre you familiar with all the different parts of the plot? Does anything look familiar from earlier exercises?
Now we want to add the logo to the plot.
With ggimage we can add an image in a separate geom_image() and position the image within the plot on the same coordinates that are also used in the original plot:
ggplot(data = penguins,
aes(x = flipper_length_mm,
y = body_mass_g)) +
geom_point(aes(color = species,
shape = species),
size = 3,
alpha = 0.8) +
scale_color_manual(values = c("darkorange","purple","cyan4")) +
labs(title = "Penguin size, Palmer Station LTER",
x = "Flipper length (mm)",
y = "Body mass (g)",
color = "Penguin species",
shape = "Penguin species") +
theme(legend.position.inside = c(0.2, 0.7),
plot.title.position = "plot",
plot.caption = element_text(hjust = 0, face= "italic"),
plot.caption.position = "plot")+
geom_image(
data = tibble(flipper_length_mm = 220, body_mass_g = 3000),
aes(image = "penguin_logo.png")
)We can then adjust the size of the logo with size:
ggplot(data = penguins,
aes(x = flipper_length_mm,
y = body_mass_g)) +
geom_point(aes(color = species,
shape = species),
size = 3,
alpha = 0.8) +
scale_color_manual(values = c("darkorange","purple","cyan4")) +
labs(title = "Penguin size, Palmer Station LTER",
x = "Flipper length (mm)",
y = "Body mass (g)",
color = "Penguin species",
shape = "Penguin species") +
theme(legend.position.inside = c(0.2, 0.7),
plot.title.position = "plot",
plot.caption = element_text(hjust = 0, face= "italic"),
plot.caption.position = "plot")+
geom_image(
data = tibble(flipper_length_mm = 220, body_mass_g = 3000),
aes(image = "penguin_logo.png"),
size = 0.5
)See how the image is restricted to the plotting area and cut off?
Adapted from Albert Rapp’s blog post on the topic.
With this technique we can spice up the plot inside ggplot geoms. Now we will pretend that the following plot is pengion related and add an image inside the geom_tiles (downloaded from flaticon).
tibble(id = 0:22, row = id %% 5, col = id %/% 5) |>
ggplot(aes(x = col, y = row)) +
geom_tile(fill = 'dodgerblue4', col = 'white', linewidth = 1) +
coord_equal() +
labs(title = 'blue waffle') +
theme_void(base_size = 18)library(ggpattern)
tibble(id = 0:22, row = id %% 5, col = id %/% 5) |>
ggplot(aes(x = col, y = row)) +
geom_tile_pattern(
fill = 'dodgerblue4',
col = 'white',
linewidth = 1,
pattern = 'image',
pattern_filename = 'penguin.png'
) +
coord_equal() +
labs(
title = 'happy waffle',
caption = 'Flaticon Icon by Flat Icons'
) +
theme_void(base_size = 18)More modifications you can make to this plot can be found in the original blog post.
With the package png we can make a r object out of the penguin logo (there are corresponding packages for jpeg files for example).
With the function rasterGrob from the grid package we can transform this r object into a ggplot compatible object.
We can then use patchwork’s ìnset_element()`function to place the image into the plot.
img <- readPNG("penguin_logo.png")
# Convert to rasterGrob for use in ggplot from package grid
img_grob <- rasterGrob(img, interpolate = TRUE)
bill_len_dep <- ggplot(data = penguins,
aes(x = bill_length_mm,
y = bill_depth_mm,
group = species)) +
geom_point(aes(color = species,
shape = species),
size = 3,
alpha = 0.8) +
geom_smooth(method = "lm", se = FALSE, aes(color = species)) +
scale_color_manual(values = c("darkorange","purple","cyan4")) +
labs(title = "Penguin bill dimensions",
x = "Bill length (mm)",
y = "Bill depth (mm)",
color = "Penguin species",
shape = "Penguin species") +
theme(legend.position = c(0.85, 0.15),
plot.title.position = "plot",
plot.caption = element_text(hjust = 0, face= "italic"),
plot.caption.position = "plot")
bill_len_dep + inset_element(img_grob, 0.6, 0.6, 1, 1)We can also use the output from rasterGrob to place plots side by side:
bill_len_dep + img_grobMore on using patchwork to insert images to ggplot plots can be found here.